package com.primi.chen.zookeeper;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * Create AT 2019/12/25 18:09:18:09
 *
 * @author Primi.Chen(Chenxiangxu)
 */
@Data
public class NodeInfo {
    private String namespace;
    private String znode;
    private String environment;
    private String host;
    private String data;
    private Integer port;
    private String seq;

    public static NodeInfo of(String znode, byte[] data) {
        NodeInfo nodeInfo = new NodeInfo();
        nodeInfo.setZnode(znode);
        int indexSplit =znode.lastIndexOf("/");
        nodeInfo.setNamespace(znode.substring(0,indexSplit));
        String[] nodeNames = StringUtils.split(znode.substring(indexSplit+1), "_");
        String[] hostPort = StringUtils.split(nodeNames[0], ":");
        nodeInfo.setHost(hostPort[0]);
        nodeInfo.setPort(Integer.decode(hostPort[1]));
        nodeInfo.setSeq(nodeNames[1]);
        if (data != null) {
            nodeInfo.setData(new String(data));
        }
        return nodeInfo;
    }

}
